//
//  MCVIPCenterTableViewCell.m
//  TLYL
//
//  Created by MC on 2017/11/22.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCVIPCenterTableViewCell.h"
#import "MCCollectionViewFlowLayout.h"
#import "MCVIPCenterCollectionViewCell.h"
#import "MCMineCellModel.h"
#define H_Header1 26

@interface MCVIPCenterTableViewCell ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>


@property (nonatomic,strong) NSMutableArray*cellMarr;

@end

@implementation MCVIPCenterTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    
    self.backgroundColor=[UIColor  clearColor];
    
    UIView * upView=[[UIView alloc]init];
    upView.backgroundColor=[UIColor whiteColor];
    [self addSubview:upView];
    [upView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.left.and.right.equalTo(self);
        make.height.mas_equalTo(H_Header1);
    }];
    
    UILabel * lab1 =[[UILabel alloc]init];
    lab1.textColor=RGB(136,136,136);
    lab1.text=@"我的等级";
    lab1.font=[UIFont systemFontOfSize:12];
    [upView addSubview:lab1];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(upView);
        make.left.equalTo(self).offset(18);
        make.width.mas_equalTo(100);
    }];
    
    UIButton * lookAllLevel = [[UIButton alloc]init];
    lookAllLevel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [lookAllLevel setTitleColor:MC_THEME_COLOR forState:UIControlStateNormal];
    [lookAllLevel setTitle:@"全部等级 >>" forState:UIControlStateNormal];
    lookAllLevel.titleLabel.font=[UIFont systemFontOfSize:12];
    [upView addSubview:lookAllLevel];
    [lookAllLevel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-5);
        make.width.mas_equalTo(100);
        make.top.bottom.equalTo(upView);
    }];
    [lookAllLevel addTarget:self action:@selector(LookAllLevel) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor=[UIColor clearColor];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(0);
        make.top.equalTo(upView.mas_bottom).offset(0.5);
//        make.bottom.equalTo(self.mas_bottom);
        make.height.mas_equalTo(G_SCREENWIDTH+1);
    }];
    
    _cellMarr=[[NSMutableArray alloc]init];
    [_cellMarr    removeAllObjects];
    
    
    
    CGFloat W = [MCVIPCenterCollectionViewCell computeHeight:nil];
    for (int i=0; i<9 ; i++) {
        CollectionModel * model0=[[CollectionModel alloc]init];
        model0.header_size=CGSizeMake(G_SCREENWIDTH, 0.0001);
        model0.item_size=CGSizeMake(W, W);
        model0.section_color=[UIColor clearColor];
        model0.section_Edge=UIEdgeInsetsMake(0, 0, 0, 0);
        model0.interitemSpacing=10;
        model0.lineSpacing=10;
        model0.isHaveHeader=YES;
        model0.id_dentifier=NSStringFromClass([MCVIPCenterCollectionViewCell class]);
        /*
         * info
         */
        model0.userInfo=nil;
        [_cellMarr addObject:model0];
    }
    
    
}

/*
 * collectionView
 */
-(UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        //创建一个layout布局类
        MCCollectionViewFlowLayout * layout = [[MCCollectionViewFlowLayout alloc]init];
        //设置布局方向为横向流布局
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView=[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor=[UIColor clearColor];
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        [_collectionView registerClass:[MCVIPCenterCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MCVIPCenterCollectionViewCell class])];
        
    }
    
    return _collectionView;
}

#pragma mark - <UICollectionViewDataSource>
// 设置headerView和footerView的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        return nil ;
    }
    return nil ;
}

//设置section的颜色
- (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout colorForSectionAtIndex:(NSInteger)section{
    if (self.cellMarr.count>0) {
        CollectionModel * model=self.cellMarr[section];
        return model.section_color;
    }else{
        return [UIColor clearColor];
    }
}

//设置item的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat W = (G_SCREENWIDTH-2)/3.0;
    CGFloat H = W;
    return CGSizeMake(W,H);
}
//定义每个Section的四边间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);//分别为上、左、下、右
}
//这个是两行cell之间的间距（上下行cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
//两个cell之间的间距（同一行的cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}

//numberOfItemsInSection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(self.cellMarr.count>0){
        CollectionModel * model=self.cellMarr[section];
        if ([model.id_dentifier isEqualToString:NSStringFromClass([MCVIPCenterCollectionViewCell class])]) {
            return self.cellMarr.count;
        }
    }
    return 0;
}

//numberOfSections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//UICollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell*  cell=nil;
    CollectionModel * model=self.cellMarr[indexPath.section];
    if (model.id_dentifier) {
        cell =[collectionView dequeueReusableCellWithReuseIdentifier:model.id_dentifier forIndexPath:indexPath];
    }
    if ([model.id_dentifier isEqualToString:NSStringFromClass([MCVIPCenterCollectionViewCell class])]) {
        
        MCVIPCenterCollectionViewCell *ex_cell=(MCVIPCenterCollectionViewCell *)cell;
        ex_cell.cellTag=(indexPath.row+1);
        if (_dataSource) {
            ex_cell.dataSource=_dataSource;
        }
    }
    return cell;
}

#pragma mark-didSelect
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==6) {
        NSString *CustomerService=[NSString stringWithFormat:@"%@",_dataSource.CustomerService];
        if ([CustomerService integerValue]==0) {
            
        }else{
            if (self.keFuBlock) {
                self.keFuBlock();
            }
        }
    }
}

-(void)setDataSource:(MCGetUserVipInfoDataModel *)dataSource{
    
    _dataSource=dataSource;
    [self.collectionView reloadData];
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    
}

+(CGFloat)computeHeight:(id)info{
    
    return G_SCREENWIDTH+H_Header1;
    
}

-(void)LookAllLevel{
    if (self.allLevelBlock) {
        self.allLevelBlock();
    }
}

@end




































































